﻿using System.IO;
using System.Windows.Controls;

namespace Windows.Toolkit
{
    public class DialogService : IDialogService
    {
        public FileDialogResult ShowOpenFileDialog(FileDialogParameters parameters)
        {
            FileDialogResult result = null;

            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = parameters.Filter;
            openFileDialog.FilterIndex = parameters.FilterIndex;

            if (openFileDialog.ShowDialog() == true)
            {
                FileInfo file = openFileDialog.File;
                if (file != null)
                {
                    result = new FileDialogResult(file.Name, file.OpenRead());
                }
            }

            return result;
        }

        public FileDialogResult ShowSaveFileDialog(FileDialogParameters parameters)
        {
            FileDialogResult result = null;

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = parameters.Filter;
            saveFileDialog.FilterIndex = parameters.FilterIndex;

            if (saveFileDialog.ShowDialog() == true)
            {
                result = new FileDialogResult(saveFileDialog.SafeFileName, saveFileDialog.OpenFile());
            }

            return result;
        }
    }
}
